package com.maistars.spider.service.parser;

import com.alibaba.fastjson.JSONObject;
import com.maistars.spider.common.constants.PageContentType;
import com.maistars.spider.common.constants.PageType;
import com.maistars.spider.common.constants.ScanType;
import com.maistars.spider.service.domain.rule.*;
import com.maistars.spider.service.parser.policy.ParserPolicy;
import com.maistars.spider.service.parser.policy.ParserPolicyType;
import com.maistars.spider.service.parser.policy.impl.ConstPolicy;
import com.maistars.spider.service.parser.policy.impl.JsonPathPolicy;
import com.maistars.spider.service.parser.policy.impl.XPathPolicy;
import com.maistars.spider.service.parser.policy.impl.XmlPathPolicy;
import com.maistars.spider.service.process.html.HtmlProcessorPipeline;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 对网页抓取规则进行解析，并返回对应的网址内容
 *
 * @author dhr
 * @date 2021/1/16 下午7:08
 */
public class PageRuleParser {

    public List<String> getRealUrls(String containerPageContent, PageTurnRule pageTurnRule) {
        List<String> realUrls = new ArrayList<>();
        // 处理分页
        if (pageTurnRule != null) {
//            if (pageTurnRule.getType() == 1) {
//                AutoPageTurnRule autoPageTurnRule = (AutoPageTurnRule) pageTurnRule;
//                int patternType = autoPageTurnRule.getPatternType();
//                String urlPattern = autoPageTurnRule.getUrlPattern();
//                return parseUrlPattern(containerPageContent, patternType, urlPattern, new HashMap<>());
//            } else {
//                ManualPageTurnRule manualPageTurnRule = (ManualPageTurnRule) pageTurnRule;
//                int patternType = 0;
//                String urlPattern = "";
//                return parseUrlPattern(containerPageContent, patternType, urlPattern, new HashMap<>());
//            }
        }
        return realUrls;
    }

    public List<String> getRealUrls(String containerPageContent, PageRule pageRule) {
        List<String> realUrls = new ArrayList<>();
        if (PageType.LIST.getValue() == pageRule.getPageType() && pageRule.getFieldRules() != null && !pageRule.getFieldRules().isEmpty()) {
            // 查询列表字段信息
            FieldRule fieldRule = pageRule.getFieldRules().get(0);
            realUrls = parseUrlPattern(containerPageContent, pageRule,fieldRule);
        }
        List<String> newRealUrls = new ArrayList<>();
        if(!CollectionUtils.isEmpty(realUrls)){
            newRealUrls.add(realUrls.get(0));
        }
        return realUrls;
    }

    private List<String> parseUrlPattern(String containerPageContent, PageRule pageRule,FieldRule fieldRule) {
        List<String> realUrls = new ArrayList<>();
        Map<String,String> extendParams = new HashMap<>();
        extendParams.put("processConfig",fieldRule.getProcessConfig());
        if (fieldRule.getParseType() == ParserPolicyType.XPATH.getValue()) {
            if(PageContentType.HTML.getValue() == pageRule.getPageContentType()){
                JSONObject processorConfig = new JSONObject();
                realUrls = HtmlProcessorPipeline.build(0,processorConfig,fieldRule,containerPageContent)
                        .execute();
            }else{
                ParserPolicy parserPolicy = new XPathPolicy();
                parserPolicy.init(containerPageContent);
                realUrls = parserPolicy.execute(fieldRule.getRule(), extendParams);
            }
        } else if (fieldRule.getParseType() == ParserPolicyType.JSONPATH.getValue()) {
            ParserPolicy parserPolicy = new JsonPathPolicy();
            parserPolicy.init(containerPageContent);
            realUrls = parserPolicy.execute(fieldRule.getRule(), extendParams);
        } else if (fieldRule.getParseType() == ParserPolicyType.XMLPATH.getValue()) {
            ParserPolicy parserPolicy = new XmlPathPolicy();
            parserPolicy.init(containerPageContent);
            realUrls = parserPolicy.execute(fieldRule.getRule(), extendParams);
        } else if (fieldRule.getParseType() == ParserPolicyType.CONSTPATH.getValue()) {
            ParserPolicy parserPolicy = new ConstPolicy();
            parserPolicy.init(containerPageContent);
            realUrls = parserPolicy.execute(fieldRule.getRule(), extendParams);
        }
        return realUrls;
    }
}
